<template>
  <div :fault="fault" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
// import { fetchgetData } from '@/api/home.js'

const animationDuration = 50

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }

  },
  data() {
    return {
      fault: [],
      chart: null
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  created() {
    this.fechData()
  },
  methods: {
    // fechData() {
    // fetchgetData().then(response => {
    //   // const allArr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
    //   const allArr = response.faultdata

    //   console.log('数据是：' + allArr)
    //   // 在方法中定义 数组，vue前台不显示，在data中直接赋值则显示
    //   var arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
    //   // 方式一
    //   this.fault = arr
    //   // 方式二
    //   for (var i = 0; i < arr.length; i++) {
    //     this.fault.push(arr[i])
    //   }

    //   console.log('数据是2222222222：' + this.fault)
    // })
    // },

    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',

          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: 'pageA',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          // 传递本年度对应的数据
          data: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23],
          animationDuration
        }]
      })
    }
  }
}
</script>
